// created by noellynch
// May 6, 2009

package com.sceneworker.editors.colorrgbaeditor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.jme.renderer.ColorRGBA;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SwingComponentFactory;
import com.myjavautils.swing.mig.MigVBox;

public class ColorRGBAEditorPanel extends JPanel implements ChangeListener {
	private		ColorRGBA		mcl_colour;
	
	private		JSlider			mcl_red;
	private		JSlider			mcl_green;
	private		JSlider			mcl_blue;
	
	public	ColorRGBAEditorPanel(ColorRGBA cl_colour, boolean b_asCopy, String str_title) {
		if(b_asCopy) {
			mcl_colour = new ColorRGBA(cl_colour);
		} else {
			mcl_colour = cl_colour;
		}
		
		MigLayoutUtils.asMigLayout(this);
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(str_title), BorderFactory.createEmptyBorder(5,5,5,5)));
		
		mcl_red = MigLayoutUtils.labelledHSlider("Red", 0, 255, (int)(255f * mcl_colour.r), this, true);
		mcl_red.addChangeListener(this);
		
		mcl_green = MigLayoutUtils.labelledHSlider("Green", 0, 255, (int)(255f * mcl_colour.r), this, true);
		mcl_green.addChangeListener(this);
		
		mcl_blue = MigLayoutUtils.labelledHSlider("Blue", 0, 255, (int)(255f * mcl_colour.r), this, true);
		mcl_blue.addChangeListener(this);
		
		JComponent		lcl_colourComp = new JComponent() {

			@Override
			protected void paintComponent(Graphics g) {
				g.setColor(new Color(mcl_colour.r, mcl_colour.g, mcl_colour.b));
				g.fillRect(0, 0, getWidth(), getHeight());
				
				g.setColor(Color.white);
				g.fillRect(0, 0, 150, 20);
				g.setColor(Color.black);
				String		lstr_temp = String.format("r(%.2f), g(%.2f), b(%.2f)", mcl_colour.r, mcl_colour.g, mcl_colour.b);
				g.drawString(lstr_temp, 2, 10);
			}

			@Override
			public Dimension getPreferredSize() {
				return new Dimension(200, 20);
			}
		};
		add(lcl_colourComp);
	}
	
	public	ColorRGBA	getRGBAValue() {
		return mcl_colour;
	}

	public void stateChanged(ChangeEvent e) {
		mcl_colour.r = (float)mcl_red.getValue() / 255f;
		mcl_colour.g = (float)mcl_green.getValue() / 255f;
		mcl_colour.b = (float)mcl_blue.getValue() / 255f;
		
		repaint();
	}
	
	
	
}
